<template>
	<!-- 页面结构 -->
	<view class="u-wrap">
		<z-paging ref="paging" v-model="dataList" @query="queryList" auto-show-back-to-top
			back-to-top-threshold="400rpx" back-to-top-with-animate auto-show-system-loading>
			<template #left>
				<!-- 左侧菜单栏 -->
				<scroll-view scroll-y class="u-tab-view menu-scroll-view">
					<view v-for="(item, index) in tabbar" :key="index" class="u-tab-item"
						:class="[current == index ? 'u-tab-item-active' : '']" @click="swichMenu(item,index)">
						<text class="u-line-1">{{item.categoryName}}</text>
					</view>
				</scroll-view>
			</template>
			<!-- 右侧内容区域 -->
			<view class="content" v-for="(item1, index) in dataList" @click="item_click(item1,index)">
				<view class="left">
					<text class="txt">{{item1.productName}}</text>
					<text class="name">{{item1.price}}</text>
				</view>
				<view class="right">
					<text class="time">{{item1.productModel}}</text>
				</view>
			</view>
		</z-paging>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted,
		nextTick,
		watch
	} from 'vue';
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		api_productCategory_get_by_product,
		api_product_page,
		api_serviceItem_add,
		api_productSku_add
	} from "../../../../apis/apis.js";
	import colorGradient from '../../../../uni_modules/uview-plus/libs/function/colorGradient.js';
	import {
		useMainStore
	} from "../../../../utils/store.js"

	const store = ref(useMainStore());
	const current = ref(0); // 当前选中的菜单项索引
	const tabbar = ref([]); // 分类数据
	const categoryId = ref(0);
	const dataList = ref([]);
	const paging = ref(null);
	const orderServiceId = ref(0);
	const serviceItemId = ref(0);

	onLoad(async (options) => {
		console.log(options);
		orderServiceId.value = options.orderServiceId;
		serviceItemId.value = options.serviceItemId;
		await get_productCategory(options.serverCgId);
		reload();
	});

	// 切换左侧菜单项并刷新右侧内容
	const swichMenu = async (item, index) => {
		nextTick(() => {
			current.value = index;
		});
		categoryId.value = item.id;
		reload();
	};

	// 刷新方法
	const reload = () => {
		nextTick(() => {
			if (paging.value) {
				paging.value.reload();
			}
		});
	};

	// 加载分类数据
	const get_productCategory = async (serverCgId) => {
		let res = await api_productCategory_get_by_product({
			serverCgId: serverCgId
		}).then(res => {
			categoryId.value = res.data[0].id;
			tabbar.value = res.data;
		});
	};

	// 加载订单分页数据
	const queryList = async (current, size) => {
		const params = {
			current: current,
			size: size,
			categoryId: categoryId.value
		};
		try {
			const res = await api_product_page(params);
			paging.value.complete(res.data.records);
		} catch (error) {
			paging.value.complete(false);
		}
	};

	//点击产品
	const item_click = async (item1, index) => {
		if (store.value.serviceItem.some(item => item.productName == item1.productName)) {
			uni.showToast({
				title: "已经选过了",
				icon: "error"
			});
			return;
		} else {
			store.value.serviceItem[0].productName = item1.productName;
			store.value.serviceItem[0].productPrice = item1.price;

			let res1 = await api_productSku_add({
				serviceItemId: serviceItemId.value,
				productNum: 1,
				standardPrice: item1.price,
				actualPrice: item1.price,
				productId: item1.id
			});
			store.value.serviceItem[0].flag = false;
			uni.navigateBack({
				delta: 2,
				succes() {
					uni.showToast({
						title: "完成!",
						icon: 'success'
					});
				}
			});
		}
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		width: 750rpx;
		height: 100vh;
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;


		.content {
			height: 144rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			width: 750rpx;
			padding: 30rpx 25rpx 16rpx 25rpx;
			border-bottom: 1rpx solid #e7e7e7;

			.left {
				height: 100%;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.txt {
					font-family: Alibaba_PuHuiTi_Light;
					font-weight: 300;
					font-size: 36rpx;
					color: $font-primary-color;
				}

				.name {
					font-family: Alibaba_PuHuiTi_Light;
					font-weight: 300;
					font-size: 32rpx;
					color: #FF602B;
				}
			}

			.right {

				.time {
					font-family: Alibaba_PuHuiTi_Thin;
					font-weight: 250;
					font-size: 32rpx;
					color: #FF602B;
				}
			}
		}
	}

	.u-search-box {
		padding: 18rpx 30rpx;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-family: Alibaba_PuHuiTi_Light;
		font-weight: 300;
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 200rpx;
		height: 100%;
	}

	.u-tab-item {
		font-family: Alibaba_PuHuiTi_Regular;
		font-weight: 400;
		height: 110rpx;
		background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		line-height: 1;
	}

	.u-tab-item-active {
		font-family: Alibaba_PuHuiTi_Medium;
		font-weight: 500;
		position: relative;
		color: #000;
		font-size: 30rpx;
		background: #fff;
	}

	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid $u-primary;
		height: 32rpx;
		left: 0;
		top: 39rpx;
	}

	.u-tab-view {
		height: 100%;
	}

	.right-box {
		background-color: rgb(250, 250, 250);
	}

	.page-view {
		padding: 16rpx;
	}

	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
	}

	.class-item:last-child {
		min-height: 100vh;
	}

	.item-title {
		font-family: Alibaba_PuHuiTi_Regular;
		font-weight: 400;
		font-size: 26rpx;
		color: $u-main-color;
	}

	.item-menu-name {
		font-family: Alibaba_PuHuiTi_Light;
		font-weight: 300;
		font-size: 30rpx;
		color: $u-main-color;
	}

	.item-container {
		display: flex;
		flex-wrap: wrap;
	}

	.thumb-box {
		width: 33.333333%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-top: 20rpx;
	}

	.item-menu-image {
		width: 120rpx;
		height: 120rpx;
		border-radius: 100rpx;
		overflow: hidden;
	}
</style>